package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.OrderPageInfo;

import com.cskaoyan.bean.order.HandleOption;
import com.cskaoyan.bean.order.OrderDetail;
import com.cskaoyan.bean.order.OrderInfo;
import com.cskaoyan.bean.goods.bo.CommentBo;
import com.cskaoyan.bean.order.WxComment;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.ast.Var;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName:OrderServiceImpl
 * @Description:TODO
 * @author:leiwenjie
 * @date:2022/9/7 14:51
 * @version:1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;



    @Override
    public CommonData<MarketOrder> query(OrderPageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());


        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();

        if (info.getUserId() != null && info.getUserId() != 0) {
            criteria.andUserIdEqualTo(info.getUserId());
        }
        if (info.getOrderSn() != null && !info.getOrderSn().isEmpty()) {
            criteria.andOrderSnLike("%" + info.getOrderSn() + "%");
        }
        if (info.getOrderId() != null && info.getOrderId() != 0) {
            criteria.andIdEqualTo(info.getOrderId());
        }
        if (info.getOrderStatusArray() != null) {
            List<Short> list = Arrays.asList(info.getOrderStatusArray());
            criteria.andOrderStatusIn(list);
        }
        if (info.getStart() != null && info.getEnd() != null) {

            criteria.andAddTimeBetween(info.getStart(), info.getEnd());
        }

        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public MarketOrderGoodsData detail(int id) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);

        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(id);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);

        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketOrder.getUserId());

        MarketOrderGoodsData data = new MarketOrderGoodsData(marketOrderGoods, marketUser, marketOrder);


        return data;
    }

    @Transactional
    @Override
    public void refund(Integer orderId, BigDecimal refundMoney) {


        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setRefundAmount(refundMoney);
        marketOrder.setOrderStatus((short) 203);
        Date date = new Date();
        marketOrder.setRefundTime(date);

        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    @Override
    public void ship(MarketOrder marketOrder) {
        marketOrder.setOrderStatus((short) 301);
        Date date = new Date();
        marketOrder.setShipTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

    }

    @Override
    public void delete(Integer orderId) {
        marketOrderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public CommonData<MarketOrder> wxQuery(OrderPageInfo info) {
       // PageHelper.startPage(info.getPage(), info.getLimit());
        PageHelper.startPage(info.getPage(), 100);
        Integer showType = info.getShowType();


        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.setOrderByClause("update_time desc");
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();

        if (showType != null && showType == 1) {
            criteria.andOrderStatusEqualTo((short) 101);
        }
        if (showType != null && showType == 2) {
            criteria.andOrderStatusEqualTo((short) 201);
        }
        if (showType != null && showType == 3) {
            criteria.andOrderStatusEqualTo((short) 301);
        }
        if (showType != null && showType == 4) {
            criteria.andOrderStatusBetween((short) 401,(short) 402);
        }

        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        List<OrderInfo> orderInfos = new ArrayList<>();




        for (MarketOrder marketOrder : marketOrders) {
            MarketOrderGoodsExample goodsExample = new MarketOrderGoodsExample();
            MarketOrderGoodsExample.Criteria goodsCriteria = goodsExample.createCriteria();
            goodsCriteria.andOrderIdEqualTo(marketOrder.getId());
            List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(goodsExample);

            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setActualPrice(marketOrder.getActualPrice());
            orderInfo.setAftersaleStatus(marketOrder.getAftersaleStatus());
            orderInfo.setId(marketOrder.getId());
            orderInfo.setOrderSn(marketOrder.getOrderSn());
            orderInfo.setGoodsList(marketOrderGoods);

            HandleOption handleOption = new HandleOption();
            HandleOption data = handleOption.data(marketOrder.getOrderStatus());

            orderInfo.setHandleOption(data);

            String statusText = OrderInfo.setStatusText(marketOrder.getOrderStatus());
            orderInfo.setOrderStatusText(statusText);
            orderInfos.add(orderInfo);
        }
        PageInfo<OrderInfo> pageInfo = new PageInfo<>(orderInfos);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public OrderDetail wxDetail(Integer orderId) {

        MarketOrderGoodsExample goodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = goodsExample.createCriteria();

        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(goodsExample);


        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);

        OrderInfo orderInfo = OrderInfo.detail(marketOrder);
        if (marketOrder.getComments() != null && marketOrder.getComments() != 0) {

            orderInfo.setHandleOption(HandleOption.data((short) 403));
        } else {
            orderInfo.setHandleOption(HandleOption.data(marketOrder.getOrderStatus()));
        }
//        if (marketOrder.getComments() != 0){
//            orderInfo.
//        }
        orderInfo.setHandleOption(HandleOption.data(marketOrder.getOrderStatus()));


        orderInfo.setOrderStatus(marketOrder.getOrderStatus());
        orderInfo.setOrderStatusText(OrderInfo.setStatusText(marketOrder.getOrderStatus()));

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderGoods(marketOrderGoods);
        orderDetail.setOrderInfo(orderInfo);
        orderDetail.setExpressInfo(null);



        return orderDetail;
    }

    @Transactional
    @Override
    public void wxRefund(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        Date date = new Date();
        marketOrder.setRefundTime(date);
        marketOrder.setEndTime(date);
        marketOrder.setOrderStatus((short) 203);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

        // 返还库存

    }

    @Override
        public void commentReply(CommentBo commentBo) {
            MarketComment marketComment = new MarketComment();
            marketComment.setId(commentBo.getCommentId());
            marketComment.setContent(commentBo.getContent());
            marketCommentMapper.insertSelective(marketComment);
        }

    @Override
    public void wxDelete(Integer orderId) {
        marketOrderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public void wxConfirm(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus((short) 401);
        Date date = new Date();
        marketOrder.setConfirmTime(date);
        marketOrder.setUpdateTime(date);

        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(orderId);

        marketOrderMapper.updateByExampleSelective(marketOrder,orderExample);

    }

    @Override
    public MarketOrderGoods goods(Integer orderId, Integer goodsId) {
        MarketOrderGoodsExample goodsExample = new MarketOrderGoodsExample();

        MarketOrderGoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(goodsExample);
        // 只需要一个
        return marketOrderGoods.get(0);


    }

    @Override
    public void insertOrderComment(WxComment comment) {
        MarketOrderGoods marketOrderGoods = marketOrderGoodsMapper.selectByPrimaryKey(comment.getOrderGoodsId());
        MarketComment marketComment = new MarketComment();
        Date date = new Date();
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketComment.setValueId(marketOrderGoods.getGoodsId());
        marketComment.setHasPicture(comment.getHasPicture());
        marketComment.setPicUrls(comment.getPicUrls());
        marketComment.setContent(comment.getContent());
        marketComment.setStar(comment.getStar());
        marketComment.setType((byte) 0);
        marketCommentMapper.insertSelective(marketComment);

//        Integer orderGoodsId = comment.getOrderGoodsId();
//
//
//        marketComment.getId();

        marketOrderGoods.setComment(marketComment.getId());
        marketOrderGoodsMapper.updateByPrimaryKeySelective(marketOrderGoods);
        MarketOrderGoods orderGoods = marketOrderGoodsMapper.selectByPrimaryKey(marketOrderGoods.getId());


        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderGoods.getOrderId());
        marketOrder.setComments((short) (marketOrder.getComments() + 1));

        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);


    }

    @Override
    public void wxCancel(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus((short) 102);
        Date date = new Date();
        marketOrder.setConfirmTime(date);
        marketOrder.setUpdateTime(date);

        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(orderId);

        marketOrderMapper.updateByExampleSelective(marketOrder,orderExample);
    }


    /*@Override
    public MarketChannelData channel() {
       // List<MarketChannel> marketChannels = marketChanelMapper.selectChannel();
       // MarketChannel.
        //return
    }*/


}
